version 19
drop _all

* change the root folder according to your computer directory
global root = "D:\WorkingPaper-Series\Township_Light\Replication_Files"  
     
global dofiles = "$root\dofile"     
global working_data = "$root\workdata"
global tables = "$root\table"
global figures = "$root\figure"

cd "$working_data"

******************
*****Pre Set******
******************

global geo_ctrl = "topography_t* slope_t* roughness_t*"
global wth_ctrl = "gaez_t* precipitation_t* sunlight_t*"
global dist_ctrl = "county_dist_t* dist_hz_t* dist_coast_t*"

global full_ctrl = "$geo_ctrl $wth_ctrl $dist_ctrl"

global geo_ctrl_1 = "topography_t_1 slope_t_1 roughness_t_1"
global wth_ctrl_1 = "gaez_t_1 precipitation_t_1 sunlight_t_1"
global dist_ctrl_1 = "county_dist_t_1 dist_hz_t_1 dist_coast_t_1"

global full_ctrl_1 = "$geo_ctrl_1 $wth_ctrl_1 $dist_ctrl_1"

global geo_ctrl_2 = "topography_t_2 slope_t_2 roughness_t_2"
global wth_ctrl_2 = "gaez_t_2 precipitation_t_2 sunlight_t_2"
global dist_ctrl_2 = "county_dist_t_2 dist_hz_t_2 dist_coast_t_2"

global full_ctrl_2 = "$geo_ctrl_2 $wth_ctrl_2 $dist_ctrl_2"

global matchvar = "county_dist topography slope gaez sunlight dist_hz dist_coast"


global pre_ctrl = "light_t road_t pop_t"

********************
***** Figure 4 *****
********************

use "township_panel_final.dta", clear

***** Define Controls interacted with full set year FE
foreach w in county_dist roughness topography slope gaez precipitation sunlight dist_coast dist_hz {
	global year_`w' = "c.`w'#i.year"
}
global ctrl_year = "$year_county_dist $year_roughness $year_topography $year_slope $year_gaez $year_precipitation $year_sunlight $year_dist_coast $year_dist_hz "

***** Define Matrix to store results
mat define mat_robust = J(26,2,0)

***** Baseline Results
reghdfe log_light village treat_trend $full_ctrl, a(county_year idcode) cl(idcode)
mat mat_robust[1,1] = e(b)[1,1]
mat mat_robust[1,2] = sqrt(e(V)[1,1])
reghdfe ihs_light village treat_trend $full_ctrl, a(county_year idcode) cl(idcode)
mat mat_robust[2,1] = e(b)[1,1]
mat mat_robust[2,2] = sqrt(e(V)[1,1])

***** Different Sample and Light Adjustment
local i = 3
foreach cond in "radius_30==1" "county_treat!=." "city_treat!=." "towncode>99&towncode<400" "year>=2012" "light>0&light<63" {
	foreach w in log_light ihs_light {
		reghdfe `w' village treat_trend $full_ctrl if `cond', a(county_year idcode) cl(countycode)
		mat mat_robust[`i',1] = e(b)[1,1]
		mat mat_robust[`i',2] = sqrt(e(V)[1,1])
		local i=`i'+1
	}
}

***** Light Adjustment with Population
local i = 15
foreach w in log_light ihs_light {
	reghdfe `w' village treat_trend $full_ctrl [aweight = log_pop], a(county_year idcode) cl(countycode)
	mat mat_robust[`i',1] = e(b)[1,1]
	mat mat_robust[`i',2] = sqrt(e(V)[1,1])
	local i=`i'+1
}

***** Using Number of TBV as alternative measure
local i = 17
foreach x in log_village ihs_village {
	foreach w in log_light ihs_light {
		reghdfe `w' `x' treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
		mat mat_robust[`i',1] = e(b)[1,1]
		mat mat_robust[`i',2] = sqrt(e(V)[1,1])
		local i=`i'+1
	}
}

***** Cluster Standard Error Adjustment
local i = 21
foreach clust in "countycode prov_year" "citycode" {
	foreach w in log_light ihs_light {
		reghdfe `w' village treat_trend $full_ctrl, a(county_year idcode) cl(`clust')
		mat mat_robust[`i',1] = e(b)[1,1]
		mat mat_robust[`i',2] = sqrt(e(V)[1,1])
		local i=`i'+1
	}
}

***** Controls Interacted with Year Dummies
local i = 25
foreach w in log_light ihs_light {
	reghdfe `w' village treat_trend $ctrl_year, a(county_year idcode) cl(countycode)
	mat mat_robust[`i',1] = e(b)[1,1]
	mat mat_robust[`i',2] = sqrt(e(V)[1,1])
	local i=`i'+1
}

svmat mat_robust

keep if mat_robust1 != .
keep mat_robust1 mat_robust2
rename (mat_robust1 mat_robust2) (coef std_err)

export delimited using "robustness.csv", replace
